namespace PickGold.Charting.Design
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Windows.Forms;

    internal class ChartCollectionEditor : CollectionEditor
    {
        private Chart _chart;
        private ITypeDescriptorContext _context;
        private CollectionEditor.CollectionForm _form;
        private string _helpTopic;

        public ChartCollectionEditor(System.Type type) : base(type)
        {
            this._helpTopic = "";
        }

        private void CollectButtons(ArrayList buttons, Control.ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                if (control is Button)
                {
                    buttons.Add(control);
                }
                if (control.Controls.Count > 0)
                {
                    this.CollectButtons(buttons, control.Controls);
                }
            }
        }

        protected override CollectionEditor.CollectionForm CreateCollectionForm()
        {
            this._form = base.CreateCollectionForm();
            PropertyGrid propertyGrid = this.GetPropertyGrid(this._form.Controls);
            if (propertyGrid != null)
            {
                propertyGrid.HelpVisible = true;
                propertyGrid.CommandsVisibleIfAvailable = true;
                propertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(this.OnPropertyChanged);
                propertyGrid.ControlAdded += new ControlEventHandler(this.OnControlAddedRemoved);
                propertyGrid.ControlRemoved += new ControlEventHandler(this.OnControlAddedRemoved);
            }
            ArrayList buttons = new ArrayList();
            this.CollectButtons(buttons, this._form.Controls);
            foreach (Button button in buttons)
            {
                if ((button.DialogResult == DialogResult.OK) || (button.DialogResult == DialogResult.Cancel))
                {
                    button.Click += new EventHandler(this.OnOkClicked);
                }
            }
            return this._form;
        }

        protected override void DestroyInstance(object instance)
        {
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            object obj2;
            this._context = context;
            if ((context != null) && (context.Instance != null))
            {
                this._chart = context.Instance as Chart;
            }
            if (this._chart != null)
            {
                Chart.ControlCurrentContext = context;
            }
            INameController controller = value as INameController;
            bool flag = (controller != null) && ((value is ChartAreaCollection) || (value is LegendCollection));
            try
            {
                if (flag)
                {
                    controller.DoSnapshot(true, null, null);
                    controller.IsColectionEditing = true;
                }
                obj2 = base.EditValue(context, provider, value);
            }
            finally
            {
                if (flag)
                {
                    controller.IsColectionEditing = false;
                    controller.DoSnapshot(false, null, null);
                }
            }
            return obj2;
        }

        public static object GetChartReference(object instance)
        {
            if (instance is Chart)
            {
                return instance;
            }
            IChartElement element = instance as IChartElement;
            if (element == null)
            {
                throw new InvalidOperationException(SR.ExceptionEditorContectInstantsIsNotChartObject);
            }
            return element.Common.Chart;
        }

        private PropertyGrid GetPropertyGrid(Control.ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                PropertyGrid propertyGrid = control as PropertyGrid;
                if (propertyGrid != null)
                {
                    return propertyGrid;
                }
                if (control.Controls.Count > 0)
                {
                    propertyGrid = this.GetPropertyGrid(control.Controls);
                    if (propertyGrid != null)
                    {
                        return propertyGrid;
                    }
                }
            }
            return null;
        }

        private void OnControlAddedRemoved(object sender, ControlEventArgs e)
        {
            if (ChartWebDesigner.controlDesigner != null)
            {
                ChartWebDesigner.controlDesigner.UpdateDesignTimeHtml();
            }
        }

        private void OnOkClicked(object sender, EventArgs e)
        {
            this._helpTopic = "";
        }

        private void OnPropertyChanged(object sender, PropertyValueChangedEventArgs e)
        {
            if (ChartWebDesigner.controlDesigner != null)
            {
                ChartWebDesigner.controlDesigner.UpdateDesignTimeHtml();
            }
        }

        protected override object SetItems(object editValue, object[] value)
        {
            object obj2 = base.SetItems(editValue, value);
            IComponentChangeService service = this._context.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
            INameController controller = editValue as INameController;
            if (((controller != null) && (service != null)) && ((editValue is ChartAreaCollection) || (editValue is LegendCollection)))
            {
                IList list = (IList) obj2;
                bool flag = false;
                foreach (ChartNamedElement element in controller.Snapshot)
                {
                    if (list.IndexOf(element) < 0)
                    {
                        flag = true;
                    }
                }
                if (!flag)
                {
                    return obj2;
                }
                service.OnComponentChanging(this._chart, null);
                ChartNamedElement newElement = (list.Count > 0) ? ((ChartNamedElement) list[0]) : null;
                foreach (ChartNamedElement element3 in controller.Snapshot)
                {
                    if (list.IndexOf(element3) < 0)
                    {
                        controller.OnNameReferenceChanged(new NameReferenceChangedEventArgs(element3, newElement));
                    }
                }
                service.OnComponentChanged(this._chart, null, null, null);
            }
            return obj2;
        }

        protected override void ShowHelp()
        {
            this._helpTopic = "";
            PropertyGrid propertyGrid = this.GetPropertyGrid(this._form.Controls);
            if (propertyGrid != null)
            {
                GridItem selectedGridItem = propertyGrid.SelectedGridItem;
                if ((selectedGridItem != null) && ((selectedGridItem.GridItemType == GridItemType.Property) || (selectedGridItem.GridItemType == GridItemType.ArrayValue)))
                {
                    this._helpTopic = selectedGridItem.PropertyDescriptor.ComponentType.ToString() + "." + selectedGridItem.PropertyDescriptor.Name;
                }
            }
            base.ShowHelp();
            this._helpTopic = "";
        }

        protected override string HelpTopic
        {
            get
            {
                if (this._helpTopic.Length != 0)
                {
                    return this._helpTopic;
                }
                return base.HelpTopic;
            }
        }
    }
}

